<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="../css/mui.css" />
		<link rel="stylesheet" href="../css/style.css" />
		<script type="text/javascript" src="../js/jquery-1.8.0.min.js" ></script>
		<script type="text/javascript" src="../js/mui.min.js" ></script>
		<script type="text/javascript" src="../js/head_public.js" ></script>
		<script type="text/javascript" src="../js/common.js" ></script>
		<script type="text/javascript" src="../js/session.js" ></script>
		<style>
			.zhifu-container  input{font-size: 14px;border: none;border-bottom: 1px solid #666;border-radius: 0px;width: 80%;display: block;margin: auto;}
		</style>
	</head>
	<body >
		<div class="login-container xiaofang-login" id="login-container">
			<img src="../images/login-biao.png" class="login-biao" />
			<form class="login-form" id="form">
				<p><input type="text"  name="member_name" placeholder="请输入用户名"/><img src="../images/login-icon1.png"></p>
				<p><input type="password" class="newpass" name="member_password" placeholder="请输入密码"/><img src="../images/login-icon3.png"></p>
				<div class="go-login-div"><a data-href="forgetpass.html">忘记密码？</a><a data-href="regiest.html">注册</a></div>
				<div class="sub-btn" style="margin-top: 1rem;"><button type="button" class="submit">确定</button></div>
				<!--<div class="fengexian" style="padding-top: 0.5rem;"><span><hr></span><span>其他登录方式</span><span><hr></span></div>
				<div>
					<ul class="sanfang-img" style="padding-top: 0.5rem;">
						<li>
							<img src="../images/login-wx.png" data-type="wx"/>
						</li>
						<li>
							<img src="../images/login-zfb.png" data-type="zhifubao"/>
						</li>
						<li>
							<img src="../images/login-qq.png" data-type="qq" />
						</li>
					</ul>
				</div>-->
			</form>
			<div class="zhifu-container zhifu-container-wx">
				<div class="menban"></div>
				<div class="zhifu-content" style="top: 100px;bottom: auto;border-radius: 0.3rem;width: 80%;left: 10%;">
					<h1 style="font-size: 14px;"><img src="../images/close.png" class="close-icon" >请填写要绑定的账号和密码</h1>
				    <input type="text" id="code" placeholder="请填写需要绑定的账号"/>
				    <input type="password" id="mima" placeholder="请填写需要绑定账号的密码"/>
					<button class="jiao-btn" style="background: #0c4b92;font-size: 14px;">确认提交</button>
				</div>
			</div>	
			<div class="zhifu-container zhifu-container-qq">
				<div class="menban"></div>
				<div class="zhifu-content" style="top: 100px;bottom: auto;border-radius: 0.3rem;width: 80%;left: 10%;">
					<h1 style="font-size: 14px;"><img src="../images/close.png" class="close-icon" >请填写要绑定的账号和密码</h1>
				    <input type="text" id="code_qq" placeholder="请填写需要绑定的账号"/>
				    <input type="password" id="mima_qq" placeholder="请填写需要绑定账号的密码"/>
					<button class="jiao-btn" style="background: #0c4b92;font-size: 14px;">确认提交</button>
				</div>
			</div>	
		</div> 
	    <script>
	    	mui.plusReady(function(){
				document.addEventListener('plusready',function(){  
				    checkArguments();  
				},false); 
			})
			// 判断启动方式  
			function checkArguments(){   
			    var args= plus.runtime.arguments;  
			    if(args){ 
				    if(args){
				    	args = unescape(args);
					    var num = args.indexOf("call=")+6;   
					    var str = args.substring(num,args.length-1);
					    var object = JSON.parse(str);
					    console.log( JSON.stringify(object));
					    
			            $.post(net+"public/index.php?s=api/Connectapily/alipay_login",object,function(res){
						    
							if(res.code=="201"){
								if(confirm("该账号未绑定注册账号，您要绑定吗？")){
									$(".zhifu-container-wx").show()
									mui(".zhifu-container-wx").on("tap",".jiao-btn",function(){
										var member_name=$("#code").val();
										var member_password=$("#mima").val();
										if(member_name==null||member_name==""){
											alert("请填写要绑定的账号");
											return false;
										}
										if(member_password==null||member_password==""){
											alert("请填写要绑定账号的密码");
											return false;
										}
										object.member_name=member_name;
										object.member_password=member_password;
										object.flag="2";
										$.post(net+"public/index.php?s=api/Connectapily/editMember",object,function(res){
											if(res.code=="200"){
												addCookie("userid",res.result.member_id,7);
												addCookie("username",res.result.member_name,7);
												addCookie("work",res.result.member_work,7);
												addCookie("workid",res.result.work_id,7);
												alert("登录成功");
											    location.href="index.html";
											}else{
												alert(res.message)
											}
										})
									})
                               }else{
                               	    console.log(object.sex)
                                	object.flag="1";
									$.post(net+"public/index.php?s=api/Connectapily/editMember",object,function(res){
										console.log(JSON.stringify(res))
										if(res.code=="200"){
											addCookie("userid",res.result.member_id,7);
											addCookie("username",res.result.member_name,7);
											addCookie("work",res.result.member_work,7);
											addCookie("workid",res.result.work_id,7);
											alert("登录成功");
										    location.href="index.html";
										}else{
												alert(res.message)
											}
									})
                                }
						   }else if(res.code=="200"){
								addCookie("userid",res.result.member_id,7);
								addCookie("username",res.result.member_name,7);
								addCookie("work",res.result.member_work,7);
								addCookie("workid",res.result.work_id,7);
								alert("登录成功");
							   location.href="index.html";
							}
						})
				    } 
			    }
			}  
			// 处理从后台恢复  
			document.addEventListener('newintent',function(){  
			    console.log("addEventListener: newintent");  
			    checkArguments();  
			},false);
			$(function() {
				mui("body").on("tap","a",function(){
					mui.openWindow({
						url:$(this).attr("data-href")
					})
				})
				mui(".zhifu-content").on("tap",".close-icon",function(){
					$(".zhifu-container").hide();
					location.reload()
				})
				mui("body").on("tap",".submit",function(){				
					
		        	var $passwd = /^\w{6,12}$/;
		            var params = $("#form").serializeObject();//将表单序列化为JSON对象
		            if(params.member_name==""||params.member_name==null){
		            	alert("请填写用户名")
		            	return false;
					}else if(params.member_password==""||params.member_password==null){
						alert("密码不能为空！");
						return false;
					}else if(!$passwd.test(params.member_password)){
						alert("密码为6-12字母或者数字！")
						return false;
					} 
					$.post(net+"public/index.php?s=api/login/login",params,function(res){
						if(res.code=="201"){
							alert(res.message)
						}else if(res.code=="200"){
							alert(res.message);
							addCookie("userid",res.result.member_id,7);
							addCookie("username",res.result.member_name,7);
							addCookie("work",res.result.member_work,7);
							addCookie("workid",res.result.work_id,7);
//							mui.openWindow({url:"index.html"})
                            location.href="index.html";
						}else{
							alert(res.message);
						}
					})
		        })  
		    });
		    $.fn.serializeObject = function() {  
		        var o = {};  
		        var a = this.serializeArray();  
		        $.each(a, function() {  
		            if (o[this.name]) {  
		                if (!o[this.name].push) {  
		                    o[this.name] = [ o[this.name] ];  
		                }  
		                o[this.name].push(this.value || '');  
		            } else {  
		                o[this.name] = this.value || '';  
		            }  
		        });  
		        return o;  
		    }
		
		</script>
		<script>
			var auths=null;
//			document.getElementById("loginByWX").addEventListener('tap', function() {
		     mui(".sanfang-img").on("tap","li img",function(){
		     	var type=$(this).attr("data-type");
		     	if(type=="zhifubao"){
					$.post(net+"public/index.php?s=api/Connectapily/apily_login",{},function(res){
						if(res.code==200){
							plus.runtime.openURL(res.result);
						}
					})
				}else {
				plus.oauth.getServices( function(services){
					auths = services;
//					for(var k in auths){
//						console.log(auths[k].id)
//					}
					//auths解释0QQ 1微信 2微博 3小米，但是不建议使用auths[1]类似的写法，因为各个设备排序不一样，比较坑爹
					//注意获取使用unionid，此id通用后期的微信端等它会用户共享，（openid完全唯一）
					//var s = auths[1];
					var weixin="";
					var qq="";
					var s="";
					for (var i = 0; i < auths.length; i++){
					//用这样的写法指定第三方，参照：
					//[LOG] : xiaomi
					//[LOG] : qq
					//[LOG] : sinaweibo
					//[LOG] : weixin			 
						if (auths[i].id == 'weixin'){
						 weixin = auths[i];
						 
						}else if (auths[i].id == 'qq'){
						 qq = auths[i];
						 break; 
						}
					}
					if(type=="wx"){
						s=weixin;
					}else if(type=="qq"){
						s=qq;
					}
						s.login(function(e){
							// 获取登录操作结果
							s.getUserInfo( function(e){
//								alert( "获取用户信息成功："+JSON.stringify(s.userInfo) );
//								alert( "获取用户信息成功："+JSON.stringify(s.authResult) );
//                              微信登录
								if (type=='wx') {
									var wx_params={};
									wx_params.unionid=s.userInfo.unionid;
									wx_params.nickname=s.userInfo.nickname;
									wx_params.openid=s.userInfo.openid;
									wx_params.sex=s.userInfo.sex;
									wx_params.headimgurl=s.userInfo.headimgurl;
									$.post(net+"/public/index.php?s=api/Connectwx/index",wx_params,function(res){
									
										if(res.code=="201"){
											if(confirm("该账号未绑定注册账号，您要绑定吗？")){
												$(".zhifu-container-wx").show()
												mui(".zhifu-container-wx").on("tap",".jiao-btn",function(){
													var member_name=$("#code").val();
													var member_password=$("#mima").val();
													if(member_name==null||member_name==""){
														alert("请填写要绑定的账号");
														return false;
													}
													if(member_password==null||member_password==""){
														alert("请填写要绑定账号的密码");
														return false;
													}
													wx_params.member_name=member_name;
													wx_params.member_password=member_password;
													wx_params.flag="2";
													$.post(net+"/public/index.php?s=api/Connectwx/editMember",wx_params,function(res){
														
														if(res.code=="200"){
															addCookie("userid",res.result.member_id,7);
															addCookie("username",res.result.member_name,7);
															addCookie("work",res.result.member_work,7);
															addCookie("workid",res.result.work_id,7);
															alert("登录成功");
														    location.href="index.html";
														}else{
															alert(res.message)
														}
													})
												})
											}else{
												wx_params.flag="1";
												$.post(net+"/public/index.php?s=api/Connectwx/editMember",wx_params,function(res){
													
													if(res.code=="200"){
														addCookie("userid",res.result.member_id,7);
														addCookie("username",res.result.member_name,7);
														addCookie("work",res.result.member_work,7);
														addCookie("workid",res.result.work_id,7);
														alert("登录成功");
													    location.href="index.html";
													}else{
														alert(res.message)
													}
												})
//												alert("登录成功！")
											}
										}else if(res.code=="200"){
											
											addCookie("userid",res.result.member_id,7);
											addCookie("username",res.result.member_name,7);
											addCookie("work",res.result.member_work,7);
											addCookie("workid",res.result.work_id,7);
											alert("登录成功");
										   location.href="index.html";
										}else{
												alert(res.message)
											}
									})
//									qq登录
								} else if(type=="qq"){
									var qq_params={};
									var sex="1";
									qq_params.qqopenid=s.authResult.openid;
									qq_params.nickname=s.userInfo.nickname;
									qq_params.openid=s.userInfo.openid;
									if(s.userInfo.gender=="男"){
										sex="1";
									}else if(s.userInfo.gender=="女"){
										sex="2";
									}
									qq_params.sex=sex;
									qq_params.headimgurl=s.userInfo.headimgurl;
									$.post(net+"/public/index.php?s=api/Connectqq",qq_params,function(res){
										if(res.code=="201"){
											if(confirm("该账号未绑定注册账号，您要绑定吗？")){
												$(".zhifu-container-qq").show()
												mui(".zhifu-container-qq").on("tap",".jiao-btn",function(){
													var member_name=$("#code_qq").val();
													var member_password=$("#mima_qq").val();
													if(member_name==null||member_name==""){
														alert("请填写要绑定的账号");
														return false;
													}
													if(member_password==null||member_password==""){
														alert("请填写要绑定账号的密码");
														return false;
													}
													qq_params.member_name=member_name;
													qq_params.member_password=member_password;
													//绑定账号
													$.post(net+"/public/index.php?s=api/Connectqq/bingqq",qq_params,function(res){
														if(res.code=="200"){
															addCookie("userid",res.result.member_id,7);
															addCookie("username",res.result.member_name,7);
															addCookie("work",res.result.member_work,7);
															addCookie("workid",res.result.work_id,7);
															alert("登录成功");
														    location.href="index.html";
														}else{
															alert(res.message)
														}
													})
												})
											}else{
//												注册新号
												$.post(net+"/public/index.php?s=api/Connectqq/register",qq_params,function(res){
													if(res.code=="200"){
															addCookie("userid",res.result.member_id,7);
															addCookie("username",res.result.member_name,7);
															addCookie("work",res.result.member_work,7);
															addCookie("workid",res.result.work_id,7);
															alert("登录成功");
															location.href="index.html";
														   
													}else{
														alert(res.message)
													}
												})
                                            //	alert("登录成功！")
											}
										}else if(res.code=="200"){
											addCookie("userid",res.result.member_id,7);
											addCookie("username",res.result.member_name,7);
											addCookie("work",res.result.member_work,7);
											addCookie("workid",res.result.work_id,7);
											alert("登录成功");
										   location.href="index.html";
										}else{
												alert(res.message)
											}
									})

								}
								mui.toast('登录成功');
							}, function(e){
								console.log( "获取用户信息失败："+e.message+" - "+e.code );
								mui.toast('获取用户信息失败');
							} );
							
						}, function(e){
							mui.toast('登录认证失败');
						});
				}, function(e){
					console.log( "获取登录失败："+e.message+" - "+e.code);
					mui.toast('登录认证失败');
				});
			}	
});
		 
		</script>
	</body>
</html>
